package cn.admin.configcenter.registry.service.client;

import cn.admin.configcenter.registry.common.config.ConfigSetting;
import cn.admin.configcenter.registry.service.AbstractServerConnectionFactory;
import cn.admin.remoting.IOEventListener;
import cn.admin.remoting.Remoting;
import cn.admin.remoting.RequestProcessor;
import cn.admin.remoting.util.ReflectionUtil;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.common.IoAcceptorConfig;

import java.util.ArrayList;
import java.util.List;

public class ConfigServerConnectionFactory extends AbstractServerConnectionFactory {

    public ConfigServerConnectionFactory() {

        List<IOEventListener> listeners = new ArrayList<>();
        listeners.add(new IncomingConnectionListener());
        List<RequestProcessor<?>> processors = new ArrayList<>();
        processors.add(new ConfigPackageProcessor());
        this.server = Remoting.serverMgr().getServer(ConfigSetting.getBindAddress(),
                9600,listeners,processors);
        IoAcceptor acceptor = (IoAcceptor) ReflectionUtil.getPrivateField(this.server,
                "acceptor");
        ((IoAcceptorConfig)acceptor.getDefaultConfig()).setDisconnectOnUnbind(false);
    }

}
